<?php

namespace app\api\controller\meet;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\meet\article\Article;
use app\common\model\meet\Banner;
use app\common\model\meet\Config;
use app\common\model\meet\External;
use app\common\model\VideoConfig;
use fast\Http;
use think\Exception;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        //轮播列表
        $banner_list = Banner::where('status', '=', Banner::STATUS_NORMAL)->field('id,image,url')->order('weigh', 'desc')->select();

        //外部链接列表
        $external_list = External::where('status', '=', External::STATUS_NORMAL)->limit(3)->order('weigh', 'desc')->select();

        //文章列表
        $article_list = Article::where('status', '=', Article::STATUS_NORMAL)->field('id,title,image')->limit(8)->order('weigh', 'desc')->select();

        //弹窗
        $pop = [
            'pop_status' => Config::getConfigByName('mini_pop_status')['value'],
            'pop_path' => Config::getConfigByName('mini_pop_path')['value'],
            'pop_image' => Config::getConfigByName('mini_pop_image')['value'] ? cdnurl(Config::getConfigByName('mini_pop_image')['value'], true) : '',
        ];

        $this->success('', ['banner_list' => $banner_list, 'external_list' => $external_list, 'article_list' => $article_list, 'pop' => $pop]);
    }

    public function getExternalList()
    {
        $page = $this->request->get('page/d', 1);
        $page_size = $this->request->get('page_size/d', 10);

        $model = new External();

        $condition = [
            'status' => $model::STATUS_NORMAL
        ];

        //外部链接列表
        $external_list = $model->where($condition)->order('weigh', 'desc')->paginate(['list_rows' => $page_size, 'page' => $page]);

        $this->success('', $external_list);
    }

    public function getConfig()
    {
        $data = [
            'mini_logo' => Config::getConfigByName('mini_logo')['value'] ? cdnurl(Config::getConfigByName('mini_logo')['value'], true) : '',
            'mini_name' => Config::getConfigByName('mini_name')['value'],
            'share_title' => Config::getConfigByName('share_title')['value'],
            'share_image' => Config::getConfigByName('share_image')['value'] ? cdnurl(Config::getConfigByName('share_image')['value'], true) : '',
            'copyright' => Config::getConfigByName('copyright')['value'],
            'first_video' => (new VideoConfig())->getConfigByName("first_video"),
            'index_video' => (new VideoConfig())->getConfigByName("index_video"),
        ];

        $this->success('', $data);
    }

    public function company()
    {
        $data = [
            'company_profile' => Config::getConfigByName('company_profile')['value'],
        ];

        $this->success('', $data);
    }

    public function search()
    {
        $data = [
            'hot_search' => Config::getConfigByName('hot_search')['value'] ? json_decode(Config::getConfigByName('hot_search')['value'], true) : []
        ];

        $this->success('', $data);
    }

    /**
     * 根据经纬度逆解析地址
     */
    public function geocoder()
    {
        $location = $this->request->get('location/s', '');

        if (empty($location)) {
            $this->error(__('Parameter %s can not be empty', 'location'));
        }

        $key = Config::getConfigByName('tencent_map_key')['value'];

        $url = "https://apis.map.qq.com/ws/geocoder/v1/?key={$key}&location={$location}";

        $response = Http::get($url);

        $result = json_decode($response, true);
        //$result
        if ($result['status'] == 0) {
            $retData['geo'] = $result['result'];
            try {
                $retData['dbRow']=$this->getDbRowByCityNameOrId($result['result']['address_component']['city']);
            }catch (Exception $exception){
                $retData['dbRow']=null;
            }
            $this->success('', $retData);
        } else {
            $this->error($result['message']);
        }
    }
    public function getDbRowByCityNameOrId($cityNameOrId,$isApi=false){
        $area =new  Area();
        $cityRow=$area->cache("cityNameOrId".$cityNameOrId,3600*24)
            ->where('name|id','=',$cityNameOrId)
            ->find();
        if (!$isApi){
            return $cityRow??null;
        }else{
            $this->success('',$cityRow);
        }

    }
}
